﻿using System.Data;
using MySql.Data.MySqlClient;

namespace Common.DataBaseHelper
{

    /// <summary>
    /// MySql数据库数据交互帮助类
    /// </summary>
    public class MySqlHelper
    {
        //在.Net Framework 下从 Web.Config文件或App.Config 文件中获取指定配置节点下的数据库连接字符串
        //private static readonly string ConStr = ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;


        /// <summary>
        /// 执行Insert,Delete,Update语句的操作
        /// </summary>
        /// <param name="con">连接对象</param>
        /// <param name="sql">需要执行的sql语句</param>
        /// <param name="cmdType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(MySqlConnection con, string sql, CommandType cmdType, params MySqlParameter[] pms)
        {

            using var cmd = new MySqlCommand(sql, con)
            {
                CommandType = cmdType
            };

            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 执行sql语句，返回单个值。
        /// </summary>
        /// <param name="con">连接对象</param>
        /// <param name="sql">要执行的sql语句</param>
        /// <param name="cmdType">需要执行的sql类型</param>
        /// <param name="pms">sql语句中的参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(MySqlConnection con, string sql, CommandType cmdType, params MySqlParameter[] pms)
        {

            using var cmd = new MySqlCommand(sql, con)
            {
                CommandType = cmdType
            };

            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            return cmd.ExecuteScalar();
        }

        /// <summary>
        /// 执行sql语句返回一个DataReader
        /// 当返回DataReader的时候，注意：
        /// 1.Connection不能关闭
        /// 2.DataReader不能关闭
        /// 3.command对象执行ExecuteReader()的时候需要传递一个参数CommandBehavior.CloseConnection
        /// </summary>
        /// <param name="con">连接对象</param>
        /// <param name="sql">需要执行的sql语句</param>
        /// <param name="cmdType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static MySqlDataReader ExecuteReader(MySqlConnection con, string sql, CommandType cmdType, params MySqlParameter[] pms)
        {
            try
            {
                using var cmd = new MySqlCommand(sql, con)
                {
                    CommandType = cmdType
                };

                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                //当调用ExecuteReader()方法的时候，如果传递一个CommandBehavior.CloseConnection参数，则表示将来当用户关闭reader的时候，系统会自动将Connection也关闭掉。
                var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch
            {
                con.Close();
                con.Dispose();
                throw;
            }

        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="sql">需要执行的一个sql语句</param>
        /// <param name="cmdType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(MySqlConnection con, string sql, CommandType cmdType, params MySqlParameter[] pms)
        {
            var sqlAdapter = new MySqlDataAdapter(sql, con)
            {
                SelectCommand = { CommandType = cmdType }
            };

            if (pms != null)
            {
                sqlAdapter.SelectCommand.Parameters.AddRange(pms);
            }
            var dt = new DataTable();
            sqlAdapter.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="sql">需要执行的一个sql语句</param>
        /// <param name="cmdType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(MySqlConnection con, string sql, CommandType cmdType, params MySqlParameter[] pms)
        {
            var sqlAdapter = new MySqlDataAdapter(sql, con)
            {
                SelectCommand = { CommandType = cmdType }
            };

            if (pms != null)
            {
                sqlAdapter.SelectCommand.Parameters.AddRange(pms);
            }

            var dataSet = new DataSet();
            sqlAdapter.Fill(dataSet);
            return dataSet;
        }
    }
}